package com.my.project.controller;

import com.my.project.model.User;
import com.my.project.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: lsg
 * @Date: Created 2018-07-14 10:52
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @RequestMapping("/registerView")
    public ModelAndView registerView(){

        ModelAndView view = new ModelAndView("register");
        return view;
    }

    @RequestMapping("/register")
    @ResponseBody
    public Map<String, Object> register(User user){

        Map<String, Object> data = new HashMap<>();
        try {
            int result = userService.addUser(user);
            if (result < 1){
                data.put("status", "fail");
                data.put("message", "注册失败！");
                return data;
            }
            data.put("status", "success");
            data.put("message", "注册成功！");
        }catch (RuntimeException e){
            e.printStackTrace();
            data.put("status", "fail");
            data.put("message", "系统异常，请稍后再试！");
        }
        return data;
    }
}
